<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\AuthRule as AuthRuleModel;
class AuthRule extends Common
{
	public function lst()
    { 
        $auth=new AuthRuleModel();

        if(request()->isPost()){
            $data=input('post.');
            //dump($data);die;
            foreach ($data as $k => $v) {
                $auth->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新成功！',url('auth_rule/lst'));
            return;
        }
        
        $authlist=$auth->authTree();
         //dump($authlist);die;
        $this->assign('authlist',$authlist);
   
        return view('list');
    }

    public function add()
    {
       $authrule=new AuthRuleModel();
        if(request()->isPost()){
            $data=input('post.');
            $plevel=$authrule->where('id',$data['pid'])->field('level')->find();
            if($plevel){
                $data['level']=$plevel['level']+1;
            }else{
                $data['level']='0';
            } 
            //dump($data);die;
            $add=$authrule->save($data);
            if($add){
                $this->success('添加权限成功！',url('auth_rule/lst'));
            }else{
                $this->error('添加权限失败！');
            }
            return;
        }
        $authres=$authrule->authTree();
        //dump($authres);die;
        $this->assign('authres',$authres);
        return view();
    }

    public function edit()
    {
        $authrule=new AuthRuleModel();
        
       if(request()->isPost()){
            // echo '你想修改',input('id'),'栏目<br/>';
            // echo '想修改他的新爹为',input('pid'),'<br />';
            // echo input('pid'),'的家谱树是：';
            // print_r($cate->getTree(input('pid')));
            // exit;
            $trees=$authrule->getTree(input('pid'));
            $flag=true;
                foreach ($trees as $v) {
                    if($v['id']==input('id')){
                        $flag=false;
                        break;
                    }
                }
                if(!$flag){
                    // echo $cat_id,'是',$data['pid'],'的祖先';
                    $this->error('上级权限选取错误！');
                    exit;

                }

            $datas=input('post.');
            $plevel=$authrule->where('id',$datas['pid'])->field('level')->find();
            if($plevel){
                $datas['level']=$plevel['level']+1;
            }else{
                $datas['level']=0;
            } 
            //dump($datas);die;
           /* $validate = \think\Loader::validate('AuthRule');//实例化link验证器
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }*/
             $save=$authrule->save($datas,['id'=>$datas['id']]);
             if($save !==false){
                $this->success('权限修改成功！',url('auth_rule/lst'));
             }else{
                $this->error('权限修改失败！');
             } 

            return;
        }
        $data=$authrule->find(input('id'));
        $authres=$authrule->authTree();
        //dump($authres);die;
        $this->assign(array(
            'data'=>$data,
           'authres'=>$authres 
           ));
          
        return view();
    }

    //前置操作
    protected $beforeActionList = [
        
        'delsancate'  =>  ['only'=>'delete'],
    ];

    //获取当前权限的子栏目
    public function delete(){
        $del=db('auth_rule')->delete(input('id'));

        if($del){
                $this->success('删除权限成功!',url('auth_rule/lst'));
            }else{
                $this->error('删除权限失败！');
            }
    }

    public function  delsancate(){
        $authruleid=input('id');//要删除的当前栏目ID
        $authrule=new AuthRuleModel();
        //$authlist=$authrule->getparentid($authruleid);
        $sonid=$authrule->getchildren($authruleid);
       
        //dump($sonid);die;
        if($sonid){
            db('auth_rule')->delete($sonid);
        }
    }
}
